package com.action.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.action.childbase.ChildBaseActivity;
import com.action.service.pressenter.SelectPacketLocationPresenter;
import com.action.ui.adapter.ShowLocationDisAdapter;
import com.action.utils.LocUtils;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.base.app_core.model.dto.LocationDto;
import com.base.app_core.model.dto.SelectDisRangeDto;
import com.base.app_core.util.AppConstants;
import com.base.app_core.util.BeanPropertiesUtil;
import com.xinglai.yun.R;
import com.tencent.map.geolocation.TencentLocation;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = AppConstants.ROUTEURI.SELECT_PACKET_LOCATION)
public class SelectPacketLocationActivity extends ChildBaseActivity<SelectPacketLocationPresenter> implements ShowLocationDisAdapter.onRangeSeleted {

    private static final int CURRENT_LOCATION = 0x01;

    @BindView(R.id.xr)
    RecyclerView xr;

    @BindView(R.id.tv_address)
    TextView tv_address;

    @BindView(R.id.tv_loc)
    TextView tv_loc;

    @Inject
    LocUtils locUtils;

    List<SelectDisRangeDto> list = new ArrayList<>();

    private ShowLocationDisAdapter showLocationDisAdapter;

    @Inject
    LocationDto locationDto;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_select_packet_location;
    }

    @Override
    protected void initView() {
        super.initView();
        setToolBar("范围/位置");
        showLocationDisAdapter = new ShowLocationDisAdapter();
        showLocationDisAdapter.setCircleHandlerInterface(this);
        xr.setLayoutManager(new GridLayoutManager(this, 4));
        xr.setAdapter(showLocationDisAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        list.add(new SelectDisRangeDto("1km", locationDto.getRange() == 1000));
        list.add(new SelectDisRangeDto("3km", locationDto.getRange() == 3000));
        list.add(new SelectDisRangeDto("5km", locationDto.getRange() == 5000));
        list.add(new SelectDisRangeDto("10km", locationDto.getRange() == 1000 * 10));
        list.add(new SelectDisRangeDto("区/县", locationDto.getRange() == 0));
        list.add(new SelectDisRangeDto("全市", locationDto.getRange() == -1));
        list.add(new SelectDisRangeDto("全省", locationDto.getRange() == -2));
        list.add(new SelectDisRangeDto("全国", locationDto.getRange() == -3));
        showLocationDisAdapter.notifyDataSetChanged(list);

        if (locationDto.getLongitude() == 0 || locationDto.getLatitude() == 0) {
            locUtils.requestLocation(this);
            locUtils.setOnLocationLisener(new LocUtils.LocationLisener() {

                @Override
                public void onLocationSuccess(TencentLocation location) {
                    locationDto.fillData(location);
                }

                @Override
                public void onLocationFailure(int error, String reason) {
                    showToast(reason);
                }
            });
        } else {
            initCenterCityInfo();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (locUtils != null) {
            locUtils.setOnLocationLisener(null);
        }
    }

    @Override
    protected String[] getPermissions() {
        return new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
    }

    @Override
    protected void setupComponent() {
        super.setupComponent();
        getChildViewComponent().inject(this);
    }

    @OnClick({R.id.ll_choose_location, R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_choose_location:
                startActivityForResult(new Intent(this, SelectedCurrentLocationActivity.class)
                                .putExtra(AppConstants.EXTRA.LOCATION_INFO, locationDto),
                        CURRENT_LOCATION);
                break;
            case R.id.submit:
                Intent intent = getIntent();
                intent.putExtra(AppConstants.EXTRA.LOCATION_INFO, locationDto);
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }

    @Override
    public void onSeleted(SelectDisRangeDto item) {
        for (SelectDisRangeDto o : list) {
            o.setSeleted(false);
        }
        item.setSeleted(true);
        locationDto.setRangeAction(item);
        showLocationDisAdapter.notifyDataSetChanged(list);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        }
        if (requestCode == CURRENT_LOCATION) {
            LocationDto l = data.getParcelableExtra(AppConstants.EXTRA.LOCATION_INFO);
            try {
                BeanPropertiesUtil.copyProperties(l, locationDto);
                initCenterCityInfo();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void initCenterCityInfo() {
        tv_loc.setText(locationDto.getName());
        tv_address.setText(locationDto.getAddress());
    }
}
